package com.baizhi.controller;




import cn.hutool.core.io.FileUtil;
import com.baizhi.entity.DdProduct;
import com.baizhi.service.impl.DdProductServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 *
 * @author xutengyang
 * @since 2020-12-14
 */
@RestController
public class DdProductController {
    @Autowired
    private DdProductServiceImpl dsi;

    //查询所有
    @GetMapping("products")
    public Map getProducts(int page, int limit){
        System.out.println(page);
        System.out.println(limit);
        Page<DdProduct> products = new Page<>(page, limit);
        dsi.page(products);
        System.out.println(products);
        return new HashMap() {{
            put("code", 0);
            put("msg", "");
            put("data", products.getRecords());
            put("count", products.getTotal());
        }};
    }

    //批量删除
    @DeleteMapping("products")
    public Map deleteProducts(@RequestBody List<Integer> ids){
        System.out.println(ids);
        boolean b = dsi.removeByIds(ids);
        return new HashMap() {{
            put("code", 0);
        }};
    }

    //上传图片
    @PostMapping("uploadImage/{bookId}")
    public Map uploadImage(@PathVariable("bookId") Integer bookId,MultipartFile file, HttpServletResponse response) throws IOException {
        System.out.println("bookId:"+bookId);
        String originalFilename = file.getOriginalFilename();
        String newName= UUID.randomUUID()+originalFilename;
        String imagePath="d:\\uploadTest\\layuiFileCopy\\"+newName;
        file.transferTo(new File(imagePath));
        QueryWrapper<DdProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("book_id",bookId);
        DdProduct product = dsi.getById(bookId);
        product.setImagepath(newName);
        product.setProductImage(originalFilename);
        boolean update = dsi.update(product,wrapper);
        System.out.println("图片更新结果："+update);
        return new HashMap(){{
            put("code",0);
            put("fileName",originalFilename);
        }};
    }

    //根据bookid查询
    @GetMapping("products/{bookId}")
    public DdProduct getById(@PathVariable("bookId")Integer bookId){
        System.out.println(bookId);
        DdProduct byId = dsi.getById(bookId);
        System.out.println(byId);
        return byId;
    }
    //图片显示
    @GetMapping("image/{imagepath}")
    public void showImage(@PathVariable("imagepath")String imagepath,MultipartFile file,HttpServletResponse response) throws IOException {
        System.out.println("被访问");
        response.setContentType("application/octet-stream;charset=UTF-8");
        File file1 = new File("d:\\uploadTest\\layuiFileCopy\\" + imagepath);
        
    }
}

